#!/bin/bash

# BUG: All files must have same number of fields and same format.

printf "\nWorking directory: $(pwd)\n"
printf "\nFormatting input files..\n"
for f in "${@:2}"
do
    echo "  $f"
    awk -F, 'NR==1{print $0;}' OFS=, $f  > "$f.tmp.header"
    # awk -F, 'NR!=1&&$1!="pid"{print $0}' OFS=, $f | perl -pe 's/(\d{2}?)\/(\d{2}?)\/(\d{4}?)/\3-\1\-\2/g' | sort -t "," -k1,1  > "$f.tmp"
    # cp $f "$f.tmp"
done


printf "\nJoining..\n"
echo "  $2"
commas2=$(awk 'NR==2' "$2" | tr -d -c ',\n' | awk '{print length+1}')
echo "  $3"
commas3=$(awk 'NR==2' "$3" | tr -d -c ',\n' | awk '{print length+1}')
leftfile=$(printf '%*s' "$commas2" | perl -pe 's/ / "1." . ++$n . " "/ge' | sed -e 's/1.1 //g')
rightfile=$(printf '%*s' "$commas3" | perl -pe 's/ / "2." . ++$n . " "/ge' | sed -e 's/2.1 //g')
join -t, -a1 -a2 -o 0 $leftfile $rightfile -1 1 -2 1 "$2" "$3"  > tmp.tmp
rm "$2.tmp"; rm "$3.tmp"
wc -l tmp.tmp


sed -e 's/:/,/g' tmp.tmp > $1
rm tmp.tmp
printf "\nDone (EOF).\n\n"


# ./merge_voterYears.sh ../CalVoter_files/formattedByIndividual_CaliAllYears_1.csv ../CalVoter_files/formattedByIndividual_Cali05.csv ../CalVoter_files/formattedByIndividual_Cali07.csv ../CalVoter_files/formattedByIndividual_Cali09.csv && ./merge_voterYears.sh ../CalVoter_files/formattedByIndividual_CaliAllYears_2.csv ../CalVoter_files/formattedByIndividual_Cali10.csv ../CalVoter_files/formattedByIndividual_Cali12.csv && ./merge_voterYears.sh ../CalVoter_files/formattedByIndividual_CaliAllYears.csv ../CalVoter_files/formattedByIndividual_CaliAllYears_1.csv ../CalVoter_files/formattedByIndividual_CaliAllYears_2.csv
